package com.controller;

import com.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginController extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String name= req.getParameter("user");
        String password= req.getParameter("password");
        String yan=req.getParameter("yanzheng");
        String yuan=req.getParameter("school");
        String xi=req.getParameter("xi");
        LoginService ls=new LoginService();
        int result=ls.check(name,password,yan,yuan,xi);
        //System.out.println(result);
        if(result==1){
            int uId=ls.getUId(name,password);
            req.getSession().setAttribute("uId",uId);
            resp.sendRedirect("main.jsp");
        }
        else{
            HttpSession session= req.getSession();
            session.setAttribute("error",ls.failure(result));
            resp.sendRedirect("loginFailure.jsp");
        }
    }
}
